#include "lv_user_ui.h"
#include "lv_user.h"
#include "app.h"
#include "io.h"


static void ui_setting_item_init(lv_obj_t * parent, uint16_t index);

const char * ui_setting_item_text[3][SETTING_ITEM_NUM + 2]=
{
    {
        "Setting",
        "Screen bri",
        "Sleep time",
        "Language",
        "Theme",
        "Hardware ver",
        "Software ver",
        "Update",
        "About",
        "Exit"
    },
    {
        "设置",
        "屏幕亮度",
        "背光时间",
        "语言",
        "主题",
        "软件版本",
        "硬件版本",
        "系统更新",
        "关于",
        "退出"
    },
    {
        "設置",
        "屏幕亮度",
        "背光時間",
        "語言",
        "主題",
        "軟件版本",
        "硬件版本",
        "系統更新",
        "關於",
        "退出",
    },
};

const void * icon_setting_item_src[]=
{
    &icon_bri,
    &icon_sleep,
    &icon_lang,
    &icon_theme,
    &icon_soft,
    &icon_hard,
    &icon_update,
    &icon_about,
};

ui_setting_page setting_page;


static void ui_event_cb(lv_event_t * e)
{
    uint8_t i;
    lv_obj_t * obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_SHORT_CLICKED)
    {
        if(obj == setting_page.btn_exit)
        {
            lv_obj_del(setting_page.main_cont);
        }
        else if(obj == setting_page.btn_setting_item[0])
        {
            printf("menu item 0 clicked\r\n");
            ui_setting_item_init(lv_scr_act(), 0);
        }
        else if(obj == setting_page.btn_setting_item[1])
        {
            printf("menu item 1 clicked\r\n");
            ui_setting_item_init(lv_scr_act(), 1);
        }
        else if(obj == setting_page.btn_setting_item[2])
        {
            printf("menu item 2 clicked\r\n");
            ui_setting_item_init(lv_scr_act(), 2);
        }
        else if(obj == setting_page.btn_setting_item[3])
        {
            printf("menu item 3 clicked\r\n");
            ui_setting_item_init(lv_scr_act(), 3);
        }
        else if(obj == setting_page.btn_setting_item[4])
        {
            ui_setting_item_init(lv_scr_act(), 4);
        }
        else if(obj == setting_page.btn_setting_item[5])
        {
            ui_setting_item_init(lv_scr_act(), 5);
        }
        else if(obj == setting_page.btn_setting_item[6])
        {
            ui_setting_item_init(lv_scr_act(), 6);
        }
        else if(obj == setting_page.btn_setting_item[7])
        {
            ui_setting_item_init(lv_scr_act(), 7);
        }
        //确认设置
        else if(obj == setting_item_page.btn_enter)
        {
            lv_obj_del(setting_item_page.main_cont);
        }
        //取消设置
        else if(obj == setting_item_page.btn_exit)
        {
            lv_obj_del(setting_item_page.main_cont);
        }
    }
}

void ui_setting_init(lv_obj_t * parent)
{
    int i;
    setting_page.main_cont = lv_obj_create(parent);
    lv_obj_set_style_pad_all(setting_page.main_cont, 0, 0);
    lv_obj_set_style_pad_gap(setting_page.main_cont, 0, 0);
    lv_obj_set_style_radius(setting_page.main_cont, 5, 0);
    lv_obj_clear_flag(setting_page.main_cont, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(setting_page.main_cont, 280, 200);
    lv_obj_align(setting_page.main_cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_border_width(setting_page.main_cont, 2, 0);
    lv_obj_set_style_border_color(setting_page.main_cont, lv_color_black(), 0);
    lv_obj_set_style_bg_color(setting_page.main_cont, lv_palette_lighten(LV_PALETTE_PURPLE, 3), 0);
    lv_obj_set_style_radius(setting_page.main_cont, 10, 0);
    
    setting_page.label_title = lv_label_create(setting_page.main_cont);
    lv_obj_set_style_text_font(setting_page.label_title, &font_siyuanheitim_20, 0);
    lv_label_set_text(setting_page.label_title, ui_setting_item_text[sys_cur_para.user_set.language][0]);
    lv_obj_align(setting_page.label_title, LV_ALIGN_TOP_MID, 0, 0);
    
    lv_obj_t * cont = lv_obj_create(setting_page.main_cont);
    lv_obj_set_style_pad_all(cont, 2, 0);
    lv_obj_set_style_pad_gap(cont, 1, 0);
    lv_obj_set_style_radius(cont, 0, 0);
    //lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(cont, 260, 162);
    lv_obj_align(cont, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_border_width(cont, 0, 0);
    lv_obj_set_style_bg_opa(cont, LV_OPA_TRANSP, 0);
    
    lv_obj_t * label;
    lv_obj_t * image;
    for(i=0;i<SETTING_ITEM_NUM;i++)
    {
        setting_page.btn_setting_item[i] = lv_btn_create(cont);
        lv_obj_set_size(setting_page.btn_setting_item[i], 240, 45);
        lv_obj_set_style_border_width(setting_page.btn_setting_item[i], 0, 0);
        lv_obj_set_style_line_width(setting_page.btn_setting_item[i], 0, 0);
        lv_obj_set_style_bg_opa(setting_page.btn_setting_item[i], LV_OPA_TRANSP, 0);
        lv_obj_clear_flag(setting_page.btn_setting_item[i], LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_pos(setting_page.btn_setting_item[i], 8, i * 50);
        lv_obj_add_event_cb(setting_page.btn_setting_item[i], ui_event_cb, LV_EVENT_ALL, NULL);
        
        image = lv_img_create(setting_page.btn_setting_item[i]);
        lv_img_set_src(image, icon_setting_item_src[i]);
        lv_obj_align(image, LV_ALIGN_LEFT_MID, 2, 0);
        //lv_img_set_zoom(image, 200);
        
        label = lv_label_create(setting_page.btn_setting_item[i]);
        lv_obj_set_style_text_font(label, &font_siyuanheitim_16, 0);
        lv_label_set_text(label, ui_setting_item_text[sys_cur_para.user_set.language][i + 1]);
        lv_obj_set_style_text_color(label, lv_color_black(), 0);
        lv_obj_align_to(label, image, LV_ALIGN_OUT_RIGHT_MID, 8, 0);
        
        image = lv_img_create(setting_page.btn_setting_item[i]);
        lv_img_set_src(image, &icon_item);
        lv_obj_align(image, LV_ALIGN_RIGHT_MID, -2, 0);
        
        
    }
    
    setting_page.btn_exit = lv_btn_create(setting_page.main_cont);
    label = lv_label_create(setting_page.btn_exit);
    lv_obj_set_style_text_font(label, &font_song_12, 0);
    lv_label_set_text(label, ui_setting_item_text[sys_cur_para.user_set.language][SETTING_ITEM_NUM + 1]);
    lv_obj_center(label);
    lv_obj_align(setting_page.btn_exit, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_add_event_cb(setting_page.btn_exit, ui_event_cb, LV_EVENT_ALL, NULL);
    
    lv_obj_add_state(setting_page.btn_setting_item[0], LV_STATE_FOCUS_KEY);
}

const char * ui_setting_item_language_text[3][5]=
{
    {
        "English",
        "Chinese simp",
        "Chinese trad",
        "Confirm",
        "Cancel"
    },
    {
        "英文",
        "中文简体",
        "中文繁体",
        "确认",
        "取消"
    },
    {
        "英文",
        "中文簡體",
        "中文繁體",
        "確認",
        "取消"
    },
};

ui_setting_item_page setting_item_page;

static void ui_setting_item_init(lv_obj_t * parent, uint16_t index)
{
    int i;
    if(index > SETTING_ITEM_NUM - 1)
    {
        return;
    }
    printf("ui item %d created!!!\r\n", index);
    lv_obj_t * label;
    setting_item_page.main_cont = lv_obj_create(parent);
    lv_obj_set_style_pad_all(setting_item_page.main_cont, 0, 0);
    lv_obj_set_style_pad_gap(setting_item_page.main_cont, 0, 0);
    lv_obj_set_style_radius(setting_item_page.main_cont, 5, 0);
    lv_obj_clear_flag(setting_item_page.main_cont, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(setting_item_page.main_cont, 310, 120);
    lv_obj_align(setting_item_page.main_cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_border_width(setting_item_page.main_cont, 2, 0);
    lv_obj_set_style_border_color(setting_item_page.main_cont, lv_color_black(), 0);
    lv_obj_set_style_bg_color(setting_item_page.main_cont, lv_palette_lighten(LV_PALETTE_PINK, 3), 0);
    lv_obj_set_style_radius(setting_item_page.main_cont, 8, 0);
    
    setting_item_page.label_title = lv_label_create(setting_item_page.main_cont);
    lv_obj_set_style_text_font(setting_item_page.label_title, &font_siyuanheitim_16, 0);
    lv_label_set_text(setting_item_page.label_title, ui_setting_item_text[sys_cur_para.user_set.language][index + 1]);
    lv_obj_align(setting_item_page.label_title, LV_ALIGN_TOP_MID, 0, 0);
    
    switch(index)
    {
        case 0: //屏幕亮度
            setting_item_page.obj_para = lv_slider_create(setting_item_page.main_cont);
            lv_slider_set_range(setting_item_page.obj_para, 5, 100);
            lv_slider_set_value(setting_item_page.obj_para, sys_cur_para.user_set.lcd_bg_pwm, LV_ANIM_ON);
            lv_obj_add_event_cb(setting_item_page.obj_para, ui_event_cb, LV_EVENT_ALL, NULL);
            break;
        case 1: //背光时间
            setting_item_page.obj_para = lv_slider_create(setting_item_page.main_cont);
            lv_slider_set_range(setting_item_page.obj_para, 5, 300);
            lv_slider_set_value(setting_item_page.obj_para, sys_cur_para.user_set.lcd_slp_tim, LV_ANIM_ON);
            lv_obj_add_event_cb(setting_item_page.obj_para, ui_event_cb, LV_EVENT_ALL, NULL);
            break;
        case 2: //语言
            setting_item_page.obj_para = lv_dropdown_create(setting_item_page.main_cont);
            lv_obj_set_style_text_font(setting_item_page.obj_para, &font_song_12, LV_PART_MAIN);
            lv_obj_set_style_text_font(lv_dropdown_get_list(setting_item_page.obj_para), &font_song_12, LV_PART_MAIN);
            lv_dropdown_clear_options(setting_item_page.obj_para);
            lv_dropdown_set_symbol(setting_item_page.obj_para, &drop_symbol);
            lv_obj_set_style_pad_all(setting_item_page.obj_para, 2, 0);
            for(i=0;i<3;i++)
            {
                lv_dropdown_add_option(setting_item_page.obj_para, \
                ui_setting_item_language_text[sys_cur_para.user_set.language][i], i);
            }
            lv_dropdown_set_selected(setting_item_page.obj_para, sys_cur_para.user_set.language);
            lv_obj_add_event_cb(setting_item_page.obj_para, ui_event_cb, LV_EVENT_ALL, NULL);
            break;
        case 3: //主题
            setting_item_page.obj_para = lv_label_create(setting_item_page.main_cont);
            lv_obj_set_style_text_font(setting_item_page.obj_para, &font_song_12, 0);
            lv_label_set_text(setting_item_page.obj_para, "暂未设计主题,敬请期待!!!");
            lv_obj_set_style_text_color(setting_item_page.obj_para, lv_color_black(), 0);
            break;
        case 4: //软件版本
            setting_item_page.obj_para = lv_label_create(setting_item_page.main_cont);
            lv_obj_set_style_text_font(setting_item_page.obj_para, &font_song_12, 0);
            lv_label_set_text(setting_item_page.obj_para, "V2.2");
            lv_obj_set_style_text_color(setting_item_page.obj_para, lv_color_black(), 0);
            break;
        case 5: //硬件版本
            setting_item_page.obj_para = lv_label_create(setting_item_page.main_cont);
            lv_obj_set_style_text_font(setting_item_page.obj_para, &font_song_12, 0);
            lv_label_set_text(setting_item_page.obj_para, "V2.1");
            lv_obj_set_style_text_color(setting_item_page.obj_para, lv_color_black(), 0);
            break;
        case 6: //系统更新
            setting_item_page.obj_para = lv_label_create(setting_item_page.main_cont);
            lv_obj_set_style_text_font(setting_item_page.obj_para, &font_song_12, 0);
            lv_label_set_text(setting_item_page.obj_para, "暂无新固件,敬请期待!!!");
            lv_obj_set_style_text_color(setting_item_page.obj_para, lv_color_black(), 0);
            break;
        case 7: //关于
            setting_item_page.obj_para = lv_label_create(setting_item_page.main_cont);
            lv_obj_set_style_text_font(setting_item_page.obj_para, &font_song_12, 0);
            lv_label_set_text(setting_item_page.obj_para, "Write by Mymymind.\nWX:xd2773128204");
            lv_obj_set_style_text_color(setting_item_page.obj_para, lv_color_black(), 0);
            break;
    }
    
    lv_obj_center(setting_item_page.obj_para);
    
    setting_item_page.btn_enter = lv_btn_create(setting_item_page.main_cont);
    label = lv_label_create(setting_item_page.btn_enter);
    lv_obj_set_style_text_font(label, &font_song_12, 0);
    lv_label_set_text(label, ui_setting_item_language_text[sys_cur_para.user_set.language][3]);
    
    setting_item_page.btn_exit = lv_btn_create(setting_item_page.main_cont);
    label = lv_label_create(setting_item_page.btn_exit);
    lv_obj_set_style_text_font(label, &font_song_12, 0);
    lv_label_set_text(label, ui_setting_item_language_text[sys_cur_para.user_set.language][4]);
    
    lv_obj_align(setting_item_page.btn_enter, LV_ALIGN_BOTTOM_LEFT, 5, -2);
    lv_obj_align(setting_item_page.btn_exit, LV_ALIGN_BOTTOM_RIGHT, -5, -2);
    
    lv_obj_add_event_cb(setting_item_page.btn_enter, ui_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(setting_item_page.btn_exit, ui_event_cb, LV_EVENT_ALL, NULL);
}
